USE [Monitoring]
GO

--|--------------------------------------------------------------------------------
--| [GetGameEntityByID] - Insert Procedure Script for GameEntity
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetGameEntityByID]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetGameEntityByID]
GO

/*CREATE PROCEDURE [dbo].[GetGameEntityByID]
(
	@ID int
)
AS
	SET NOCOUNT ON

	SELECT
		[ID],
		[GameID],
		[EntityID],
		[ParentGameEntityID],
		[GameRoleID],
		[Visible]
	FROM 
GameEntity
	WHERE
		[ID] = @ID
	
*/
GO


--|--------------------------------------------------------------------------------
--| [InsertGameEntity] - Insert Procedure Script for GameEntity
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[InsertGameEntity]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[InsertGameEntity]
GO

/*CREATE PROCEDURE [dbo].[InsertGameEntity]
(
	@ID int = NULL OUTPUT,
	@GameID int,
	@EntityID int,
	@ParentGameEntityID int = NULL,
	@GameRoleID int,
	@Visible bit
)
AS
	SET NOCOUNT ON

	INSERT INTO [GameEntity]
	(
		[GameID],
		[EntityID],
		[ParentGameEntityID],
		[GameRoleID],
		[Visible]
	)
	VALUES
	(
		@GameID,
		@EntityID,
		@ParentGameEntityID,
		@GameRoleID,
		@Visible
	)

	SELECT @ID = SCOPE_IDENTITY();

	RETURN @@Error
*/
GO

--|--------------------------------------------------------------------------------
--| [UpdateGameEntity] - Update Procedure Script for GameEntity
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[UpdateGameEntity]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
	DROP PROCEDURE [dbo].[UpdateGameEntity]
GO

/*CREATE PROCEDURE [dbo].[UpdateGameEntity]
(
	@ID int,
	@GameID int,
	@EntityID int,
	@ParentGameEntityID int = NULL,
	@GameRoleID int,
	@Visible bit
)
AS
	SET NOCOUNT ON
	
	UPDATE [GameEntity]
	SET
		[GameID] = @GameID,
		[EntityID] = @EntityID,
		[ParentGameEntityID] = @ParentGameEntityID,
		[GameRoleID] = @GameRoleID,
		[Visible] = @Visible
	WHERE 
		[ID] = @ID

	RETURN @@Error
*/
GO

--|--------------------------------------------------------------------------------
--| [DeleteGameEntity] - Update Procedure Script for GameEntity
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[DeleteGameEntity]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
	DROP PROCEDURE [dbo].[DeleteGameEntity]
GO

/*CREATE PROCEDURE [dbo].[DeleteGameEntity]
(
	@ID int
)
AS
	SET NOCOUNT ON

	DELETE 
	FROM   [GameEntity]
	WHERE  
		[ID] = @ID

	RETURN @@Error
*/

GO


--|--------------------------------------------------------------------------------
--| [GetRootGameEntityList] - Insert Procedure Script for GameEntity
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[GetRootGameEntityList]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) 
  DROP PROCEDURE [dbo].[GetRootGameEntityList]
GO

CREATE PROCEDURE [dbo].[GetRootGameEntityList]
(
	@Login nvarchar(50),
	@Password nvarchar(50),
	@GameID int
)
AS
	SET NOCOUNT ON
	SELECT
		E.ID, 
		E.Name, 
		E.Description, 
		E.TeamID, 
		E.EntityTypeID
	FROM [Entity] E 
	JOIN [GameEntity] GE ON E.ID = GE.EntityID
	JOIN [Game] G ON G.ID = GE.GameID
	JOIN [Team] T ON G.TeamID = T.ID
	JOIN [User] U ON U.TeamID = T.ID
	WHERE
		G.[ID] = @GameID 
		AND U.[Login] = @Login AND U.[Password] = @Password
		AND GE.Visible = 1
		AND GE.ParentGameEntityID IS NULL
GO

